<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加轮播图' : '编辑轮播图'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
    >
      <el-form :rules="rules" :model="bannerform" ref="bannerInfo">
        <el-form-item label="标题：" :label-width="formLabelWidth" prop="title">
          <el-input v-model="bannerform.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片：" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove="handleRemove"
            :auto-upload="false"
            :on-change="onChange"
            :file-list="fileList"
            :limit="1"
            :on-exceed="onExceed"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="状 态：" :label-width="formLabelWidth">
          <el-switch
            v-model="bannerform.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="dialogInfo.isAdd" type="primary" @click="addBanner"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="editBanner">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postAddBanner, getBannerInfo, postBannerEdit } from "../../util/axios";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      formLabelWidth: "100px",
      bannerform: {
        title: "",
        img: "",
        status: 1,
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 2, max: 15, message: "长度在 2~15个字符", trigger: "blur" },
        ],
      },
      dialogVisible: false,
      dialogImageUrl: "",
      imgUrl: "",
      fileList: [],
    };
  },
  props: ["dialogInfo"],
  methods: {
    onExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    onPreview(file) {
      console.log(file, "文件");
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleRemove(file, fileList) {
      console.log(file, "移除", fileList);
    },
    onChange(file, fileList) {
      console.log(file, "文件详情，change时候");
      this.imgUrl = file.raw;
    },
    ...mapActions({
      getBannerListAction: "banner/getBannerListAction",
    }),
    reset() {
      this.bannerform = {
        title: "",
        img: "",
        status: 1,
      };
      this.$refs.bannerInfo.resetFields();
      this.fileList = [];
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    lookup(uid) {
      getMemberInfo({ uid }).then((res) => {
        this.bannerform = res.data.list;
        this.bannerform.uid = uid;
      });
    },
    addBanner() {
      this.$refs.bannerInfo.validate((valid) => {
        if (valid) {
          this.bannerform.img = this.imgUrl;
          let file = new FormData();
          for (let i in this.bannerform) {
            file.append(i, this.bannerform[i]);
          }
          postAddBanner(file).then((res) => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.cancel();
              this.getBannerListAction();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败！");
        }
      });
    },
    lookup(id) {
      getBannerInfo({ id }).then((res) => {
        if (res.data.code == 200) {
          this.bannerform = res.data.list;
          this.bannerform.id = id;
          this.fileList = this.bannerform.img
            ? [{ url: this.$imgUrl + this.bannerform.img }]
            : [];
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    editBanner() {
      this.$refs.bannerInfo.validate((valid) => {
        if (valid) {
          this.bannerform.img = this.imgUrl;
          let file = new FormData();
          for (let i in this.bannerform) {
            file.append(i, this.bannerform[i]);
          }
          postBannerEdit(file).then((res) => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.cancel();
              this.getBannerListAction();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败！");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>
